﻿//------------------------------------------------------------------------------
//脚本名称：SetGetValues.js
//创建者  ：席吉波
//创建日期：2011-01-18
//用途    ：1.用于客户端绑定控件对页面HTML常见元素进行取值/赋值操作
//          2.调用webService
//------------------------------------------------------------------------------

function GetControl(controlId, isParent) {
    if (isParent) {
        return window.parent.document.getElementById(controlId);
    }
    else {
        return $get(controlId);
    }
}

function SetControlValue(controlId,val,isParent) {
    var control = GetControl(controlId, isParent);
    var ctrlType = control.tagName;
    switch (ctrlType) {
        case 'SELECT':
            _setDropDownListValue(controlId, val, isParent);
            break;
    }
}

//----------------------------------华丽丽的分割线------------------------------------
//赋值 开始
//
///<summary>
///给下拉列表框赋值
///</summary>
///<param name="controlId">控件ID</param>
///<param name="val">值</param>
function _setDropDownListValue(controlId, val, isParent) {
    var ddl = GetControl(controlId, isParent);
    if (ddl) {
        for (i = 0; i < ddl.options.length; i++) {
            if (val == ddl.options[i].value) {
                ddl.options[i].selected = true;
                break;
            }
        }
    }
}

///<summary>
///给一般具有value属性的非特殊控件赋值
///</summary>
///<param name="controlId">控件ID</param>
///<param name="val">值</param>
function _setNormalValue(controlId, val, isParent) {
    var control = GetControl(controlId, isParent);
    if (control) {
        control.value = val;
    }
}
///<summary>
///对<span>标签元素赋值
///</summary>
///<param name="controlId">控件ID</param>
///<param name="val">值</param>
function _setSpanValue(controlId, val, isParent) {
    var control = GetControl(controlId, isParent);
    if (control) {
        control.innerHTML = val;
    }
}

///<summary>
///给 复选框 赋值
///</summary>
///<param name="controlId">控件ID</param>
///<param name="val">值</param>
function _setChecked(controlId, val, isParent) {
    var control = GetControl(controlId, isParent);
    if (control) {
        control.checked = val == 1 ? true : false;
    }
}

///<summary>
///给代码选择控件赋值
///</summary>
///<param name="controlId">控件ID</param>
///<param name="val">值 为Code值</param>
function _setCodeNameSelectValue(controlId, val, isParent) {
    var control = GetControl(controlId + '_txtCode',isParent);
    if (control) {
        control.value = val;
        control.focus();
    }
}
///<summary>
///给代码选择控件赋值
///</summary>
///<param name="controlId">控件ID</param>
///<param name="val">值 格式为：Name(Code)</param>
function _setCodeNameSelectText(controlId, val, isParent) {
    var control = GetControl(controlId + '_txtCode',isParent);
    if (control) {
        var _clientStateControl = GetControl(controlId + '_ClientState',isParent);
        //文本输入框赋值
        control.value = val;
        
        _setSpanValue(controlId + '_Span', val,isParent);

        if (val.endsWith(')')) {
            var codename = val.trim();
            var code = codename.substr(codename.lastIndexOf("(") + 1, codename.length - 3);
            var name = codename.substr(0, codename.lastIndexOf("("));

            if (code.endsWith(')')) {
                code = code.substr(0, code.length - 1);
            }

            var state = [{
                Code: code,
                Name: name
            }];
            _clientStateControl.value = Sys.Serialization.JavaScriptSerializer.serialize(state);
        }
        else {
            var state = [{
                Code: "",
                Name: ""
            }];
            _clientStateControl.value = Sys.Serialization.JavaScriptSerializer.serialize(state);
        }
    }
}
//赋值 结束
//----------------------------------华丽丽的分割线------------------------------------
//取值 开始

function _getControlValue(controlID) {
    var ctl = $get(controlID);
    var val ="";
    if (ctl) {
        switch (ctl.tagName) {
            case "INPUT":
                switch (ctl.type) {
                    case "Text":
                    case "hidden":
                    case "button":
                        val = _getNormalValue(controlID);
                        break;
                    case "check":
                        val = _getChecked(controlID);
                        break;
                    default:
                        break;
                }
            case "SELECT":
                val = _getDropDownListValue(controlID);
                break;
            case "":
            default:
            val ="";
            break;
        }
    }
    return val;
}

///<summary>
///对DropDownList控件取Text值
///</summary>
///<param name="controlId">控件ID</param>
///<returns>值</returns>
function _getDropDownListText(controlId) {
    var ddl = $get(controlId);
    if (ddl) {
        return ddl.options[ddl.options.selectedIndex].text;
    }
    return "";
}

///<summary>
///对DropDownList控件取Value值
///</summary>
///<param name="controlId">控件ID</param>
///<returns>值</returns>
function _getDropDownListValue(controlId) {
    var ddl = $get(controlId);
    if (ddl) {
        return ddl.options[ddl.options.selectedIndex].value;
    }
    return "";
}

///<summary>
///对一般具有value属性的非特殊控件取值
///</summary>
///<param name="controlId">控件ID</param>
///<returns>值</returns>
function _getNormalValue(controlId) {
    var control = $get(controlId);
    if (control) {
        return control.value;
    }
    return "";
}
///<summary>
///对<span>标签元素取值
///</summary>
///<param name="controlId">控件ID</param>
///<returns>值</returns>
function _getSpanValue(controlId) {
    var control = $get(controlId);
    if (control) {
        return control.innerHTML;
    }
    return "";
}

///<summary>
///对 复选框 取值
///</summary>
///<param name="controlId">控件ID</param>
///<returns>值</returns>
function _getChecked(controlId) {
    var control = $get(controlId);
    if (control) {
        return control.checked  ? "1" : "0";
    }
    return "";
}

///<summary>
///对 代码选择控件取Code值
///</summary>
///<param name="controlId">控件ID</param>
///<returns>值</returns>
function _getCodeNameSelectValue(controlId) {
    var control = $get(controlId + '_ClientState');
    if (control) {
        var obj = Sys.Serialization.JavaScriptSerializer.deserialize(control.value);
        return obj[0].Code;
    }
    return "";
}
///<summary>
///对 代码选择控件取Code值
///</summary>
///<param name="controlId">控件ID</param>
///<returns>值</returns>
function _getCodeNameSelectValues(controlId) {
    var control = $get(controlId + '_ClientState');
    if (control) {
        var obj = Sys.Serialization.JavaScriptSerializer.deserialize(control.value);
        var result = "";
        for (var index in obj) {
            result+=obj[index].Code+","
        }
        if (result != "")
            result = result.substring(0, result.length - 1);
        return result;
    }
    return "";
}

///<summary>
///对 代码选择控件取Name值
///</summary>
///<param name="controlId">控件ID</param>
///<returns>值</returns>
function _getCodeNameSelectName(controlId) {
    var control = $get(controlId + '_ClientState');
    if (control) {
        var obj = Sys.Serialization.JavaScriptSerializer.deserialize(control.value);
        return obj[0].Name;
    }
    return "";
}


///<summary>
///对 代码选择控件取Text值
///</summary>
///<param name="controlId">控件ID</param>
///<returns>值</returns>
function _getCodeNameSelectText(controlId) {
    var control = $get(controlId + '_txtCode');
    if (control) {
        return control.value;
    }
    return "";
}
//----------------------------------华丽丽的分割线------------------------------------
//$Kevin Common

(function () {
    function execute(){

    KevinCommon =function(){
        this._webRequest=null;
    }

    KevinCommon.prototype={
//----------------------------------华丽丽的分割线------------------------------------
//事件处理

//----------------------------------华丽丽的分割线------------------------------------
//获取控件
        GetControl:function (controlId, isParent) {
            if (isParent) {
                return window.parent.document.getElementById(controlId);
            }
            else {
                return $get(controlId);
            }
        },

        SetControlValue:function (controlId,val,isParent) {
            var control = GetControl(controlId, isParent);
            var ctrlType = control.tagName;
            switch (ctrlType) {
                case 'SELECT':
                    _setDropDownListValue(controlId, val, isParent);
                    break;
            }
        },

//----------------------------------华丽丽的分割线------------------------------------
//赋值 开始
//
        ///<summary>
        ///给下拉列表框赋值
        ///</summary>
        ///<param name="controlId">控件ID</param>
        ///<param name="val">值</param>
        _setDropDownListValue:function (controlId, val, isParent) {
            var ddl = GetControl(controlId, isParent);
            if (ddl) {
                for (i = 0; i < ddl.options.length; i++) {
                    if (val == ddl.options[i].value) {
                        ddl.options[i].selected = true;
                        break;
                    }
                }
            }
        },

        ///<summary>
        ///给一般具有value属性的非特殊控件赋值
        ///</summary>
        ///<param name="controlId">控件ID</param>
        ///<param name="val">值</param>
        _setNormalValue:function (controlId, val, isParent) {
            var control = GetControl(controlId, isParent);
            if (control) {
                control.value = val;
            }
        },
        ///<summary>
        ///对<span>标签元素赋值
        ///</summary>
        ///<param name="controlId">控件ID</param>
        ///<param name="val">值</param>
        _setSpanValue:function (controlId, val, isParent) {
            var control = GetControl(controlId, isParent);
            if (control) {
                control.innerHTML = val;
            }
        },

        ///<summary>
        ///给 复选框 赋值
        ///</summary>
        ///<param name="controlId">控件ID</param>
        ///<param name="val">值</param>
        _setChecked:function (controlId, val, isParent) {
            var control = GetControl(controlId, isParent);
            if (control) {
                control.checked = val == 1 ? true : false;
            }
        },

        ///<summary>
        ///给代码选择控件赋值
        ///</summary>
        ///<param name="controlId">控件ID</param>
        ///<param name="val">值 为Code值</param>
        _setCodeNameSelectValue:function (controlId, val, isParent) {
            var control = GetControl(controlId + '_txtCode',isParent);
            if (control) {
                control.value = val;
                control.focus();
            }
        },
        ///<summary>
        ///给代码选择控件赋值
        ///</summary>
        ///<param name="controlId">控件ID</param>
        ///<param name="val">值 格式为：Name(Code)</param>
        _setCodeNameSelectText:function (controlId, val, isParent) {
            var control = GetControl(controlId + '_txtCode',isParent);
            if (control) {
                var _clientStateControl = GetControl(controlId + '_ClientState',isParent);
                //文本输入框赋值
                control.value = val;
        
                _setSpanValue(controlId + '_Span', val,isParent);

                if (val.endsWith(')')) {
                    var codename = val.trim();
                    var code = codename.substr(codename.lastIndexOf("(") + 1, codename.length - 3);
                    var name = codename.substr(0, codename.lastIndexOf("("));

                    if (code.endsWith(')')) {
                        code = code.substr(0, code.length - 1);
                    }

                    var state = [{
                        Code: code,
                        Name: name
                    }];
                    _clientStateControl.value = Sys.Serialization.JavaScriptSerializer.serialize(state);
                }
                else {
                    var state = [{
                        Code: "",
                        Name: ""
                    }];
                    _clientStateControl.value = Sys.Serialization.JavaScriptSerializer.serialize(state);
                }
            }
        },
//赋值 结束
//----------------------------------华丽丽的分割线------------------------------------
//取值 开始

        getControlValue:function (controlID) {
            var ctl = $get(controlID);
            var val ="";
            if (ctl) {
                switch (ctl.tagName) {
                    case "INPUT":
                        switch (ctl.type) {
                            case "text":
                            case "hidden":
                            case "button":
                                val = this.getNormalValue(controlID);
                                break;
                            case "checkbox":
                                val = this.getChecked(controlID);
                                break;
                            default:
                                break;
                        }
                        break;
                    case "SELECT":
                        val = this.getDropDownListValue(controlID);
                        break;
                    case "SPAN":
                        val = this.getSpanValue(controlID);
                        break;
                    case "":
                    default:
                    val ="";
                    break;
                }
            }
            return val;
        },

        ///<summary>
        ///对DropDownList控件取Text值
        ///</summary>
        ///<param name="controlId">控件ID</param>
        ///<returns>值</returns>
        getDropDownListText:function (controlId) {
            var ddl = $get(controlId);
            if (ddl) {
                return ddl.options[ddl.options.selectedIndex].text;
            }
            return "";
        },

        ///<summary>
        ///对DropDownList控件取Value值
        ///</summary>
        ///<param name="controlId">控件ID</param>
        ///<returns>值</returns>
        getDropDownListValue:function (controlId) {
            var ddl = $get(controlId);
            if (ddl) {
                return ddl.options[ddl.options.selectedIndex].value;
            }
            return "";
        },

        ///<summary>
        ///对一般具有value属性的非特殊控件取值
        ///</summary>
        ///<param name="controlId">控件ID</param>
        ///<returns>值</returns>
        getNormalValue:function (controlId) {
            var control = $get(controlId);
            if (control) {
                return control.value;
            }
            return "";
        },
        ///<summary>
        ///对<span>标签元素取值
        ///</summary>
        ///<param name="controlId">控件ID</param>
        ///<returns>值</returns>
        getSpanValue:function (controlId) {
            var control = $get(controlId);
            if (control) {
                return control.innerHTML;
            }
            return "";
        },

        ///<summary>
        ///对 复选框 取值
        ///</summary>
        ///<param name="controlId">控件ID</param>
        ///<returns>值</returns>
        getChecked:function (controlId) {
            var control = $get(controlId);
            if (control) {
                return control.checked  ? "1" : "0";
            }
            return "";
        },

        ///<summary>
        ///对 代码选择控件取Code值
        ///</summary>
        ///<param name="controlId">控件ID</param>
        ///<returns>值</returns>
        getCodeNameSelectValue:function (controlId) {
            var control = $get(controlId + '_ClientState');
            if (control) {
                var obj = Sys.Serialization.JavaScriptSerializer.deserialize(control.value);
                return obj[0].Code;
            }
            return "";
        },


        ///<summary>
        ///对 代码选择控件取Name值
        ///</summary>
        ///<param name="controlId">控件ID</param>
        ///<returns>值</returns>
        getCodeNameSelectName:function (controlId) {
            var control = $get(controlId + '_ClientState');
            if (control) {
                var obj = Sys.Serialization.JavaScriptSerializer.deserialize(control.value);
                return obj[0].Name;
            }
            return "";
        },
        ///<summary>
        ///对 代码选择控件取Text值
        ///</summary>
        ///<param name="controlId">控件ID</param>
        ///<returns>值</returns>
        getCodeNameSelectText:function(controlId) {
            var control = $get(controlId + '_txtCode');
            if (control) {
                return control.value;
            }
            return "";
        },
        invokeService:function(servicePath,webMethod,paras,onfailed,onSuccessed){
            if (servicePath && webMethod) {    
                           
                    if (this._webRequest) {                        
                        this._webRequest.get_executor().abort();
                        this._webRequest = null;
                    }

                    this._webRequest = Sys.Net.WebServiceProxy.invoke(servicePath, webMethod, false, paras,
                                                        Function.createDelegate(this, onSuccessed),
                                                        Function.createDelegate(this, onfailed),
                                                        null);
                }
        },
        execFunByName:function(functionName, context,paras ) {
              var args = Array.prototype.slice.call(arguments).splice(2);
              var namespaces = functionName.split(".");
              var func = namespaces.pop();
              for(var i = 0; i < namespaces.length; i++) {
                context = context[namespaces[i]];
              }
              return context[func].call(context, paras);
        }

    }

    $KevinXi=new KevinCommon();

}//end exec
if(!window.$Kevin){

    execute();
}
})();
var $KevinXi;